package Action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import Beans.Estate;
import DAO.EstateDao;
import DAO.BranchDao;

@SuppressWarnings("serial")
public class EstateAction extends ActionSupport implements ModelDriven<Estate>{
	
	private List<Estate> estates;

	EstateDao estateDao = new EstateDao();
	BranchDao branchDao = new BranchDao();

	private int estateID;
	private Estate estate = new Estate();
	
	@Override
	public Estate getModel() {
		return estate;
	}
	
	/**
	 * 编辑物业公司
	 * 
	 * @return
	 */
	public String editEstate() {
		estate.setId(estateID);
		estateDao.edit(estate);
		return getAllEstate();
	}

	/**
	 * 删除物业公司(分配终端则不可删除)
	 * 
	 * @return
	 */
	public String deleteEstate() {
		if (branchDao.getBranchByEstateId(estateID).size()>0) {
		} else {
		estate.setId(estateID);
		estateDao.delete(estate);
		}
		return getAllEstate();
	}

	/**
	 * 新增物业公司
	 * 
	 * @return
	 */
	public String registerEstate() {
		estateDao.save(estate);
		return "add";
	}
	
	/**
	 * 通过ID获取物业公司
	 */
	public String getEstateById() {
		estate=estateDao.getEstateById(estateID);
		return "edit";
	}
	
	/**
	 * 获取所有物业公司
	 */
	public String getAllEstate() {
		estates=estateDao.getAllEstate();
		return "list";
	}

	public List<Estate> getEstates() {
		return estates;
	}

	public void setEstates(List<Estate> estates) {
		this.estates = estates;
	}

	public int getEstateID() {
		return estateID;
	}

	public void setEstateID(int estateID) {
		this.estateID = estateID;
	}

	public Estate getEstate() {
		return estate;
	}

	public void setEstate(Estate estate) {
		this.estate = estate;
	}
}
